<?php
/**
 * Created by PhpStorm.
 * User: zhan
 * Date: 2018/8/22  14:43
 */
namespace app\admin\controller;
use think\Db;

class AppDaikuan extends Common
{
    /*
  *	构造函数
  */
    function __construct(){
        parent::__construct();
    }

    public function lists()
    {

       // $type = input('type');
        $pageParam = ['query' =>[]];
       // $where['type'] = $type;
        //$this->assign('type', $type);
        $status =input("status");
        if($status!=''){
            $where['status']=$status;
            $pageParam['query']['status'] = $status;
        }
        $this->assign('status', $status);
        //{描述,利率,期限,额度,人数} {"subtitle":"","interest":"","term":"","quota";"","number":""}

        $data = db('app_daikuan')->paginate(15, false, $pageParam);
       // var_dump($data);exit();
        $this->assign('list', $data);
        $this->assign('page', $data->render());

        $templet='app_daikuan/lists';
       // var_dump($tep);exit();
        return $this->fetch($templet);
    }

    public function add()
    {
        //return $this->fetch('app_daikuan/edit');exit;
        $type = input('type');

        $da['type'] = $type;
        //var_dump($type);exit();
        $this->assign('da', $da);
       // $dir=$this->checkTemplate($temptel);
        $templet='app_daikuan/edit';
        return $this->fetch($templet);
    }

    public function edit()
    {
        if(request()->isAjax()){
            $type = input('type');
            $url = input('url');
            $title = input('title');
            $sort = input('sort');
            $status = input('status');
            $subtitle = input('subtitle');
            $interest = input('interest');
            $term = input('term');
            $quota = input('quota');
            $number = input('number');
            $id = input('id');
            $data = [
                'type'=>$type,
                'url'=>$url,
                'title'=>$title,
                'subtitle'=>$subtitle,
                'term'=>$term,
                'interest'=>$interest,
                'quota'=>$quota,
                'number'=>$number,
                'sort'=>$sort,
                'status'=>$status,
            ];
            $file = request()->file('img');
            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public/static/uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    $data['img'] =  $info->getSaveName();
                    if(!empty($id)){
                        $oldimg = db('app_daikuan')->where(['id'=>$id])->value('img');
                        $oldimgurl = ROOT_PATH . 'public/static/uploads/'.$oldimg;
                        if(file_exists($oldimgurl)){
                            unlink($oldimgurl);
                        }
                    }
                }else{
                    // 上传失败获取错误信息
                  $this->error($file->getError());
                }
            }
            if(!empty($id)){
                //修改
                $res = db('app_daikuan')->where(['id'=>$id])->update($data);
            }else{
                //添加
                $res = db('app_daikuan')->insert($data);
            }
            if(!$res)$this->error('操作失败！');
            $this->success('操作成功！', url('AppDaikuan/lists'));
        }

        $id = input('id/d');
        $data = db('app_daikuan')->where(['id'=>$id])->find();
        $this->assign('da',$data);
        return $this->fetch();
    }

    public function delete()
    {
        $id = input('id');
        if(!$id)$this->error('参数有误');
        $data = db('app_daikuan')->where(['id'=>$id])->find();
        if(!$data)$this->error('数据不存在');
        $img = ROOT_PATH . 'public/static/uploads/'.$data['img'];
        if($data['img'] || is_file($img)){
            unlink($img);
        }
        $res = db('app_daikuan')->where(['id'=>$id])->delete();
        if($res)$this->success('操作成功！');
        $this->error('操作失败！');
    }
}